#include "queue.h"
#include<assert.h>

void QueueInit(Queue* q)
{
	assert(q);
	q->_front = q->_rear = nullptr;
}

void QueuePush(Queue* q, QDataType data)
{	
	assert(q);
	QNode* newnode = new QNode;
	newnode->_data = data;
	newnode->_next = nullptr;
	if (q->_front == nullptr)
	{
		q->_front = q->_rear = newnode;
	}
	else
	{
		q->_rear->_next = newnode;
		q->_rear = newnode;
	}
}

void QueuePop(Queue* q)
{
	assert(q && q->_front);
	QNode* tmp = q->_front;
	q->_front = q->_front->_next;
	if (q->_front == nullptr)
	{
		q->_front = q->_rear = nullptr;
	}
	delete tmp;
}

QDataType QueueFront(Queue* q)
{
	assert(q&&q->_front!=nullptr);
	QDataType front = q->_front->_data;
	return front;
	
}

QDataType QueueBack(Queue* q)
{
	assert(q && q->_rear != nullptr);
	QDataType rear = q->_rear->_data;
	return rear;
}

int QueueSize(Queue* q)
{
	if (q == nullptr || q->_front == nullptr) { // 检查队列指针和队首是否有效
		return 0;
	}

	int count = 0;
	QNode* current = q->_front; // 使用临时指针current进行遍历

	while (current != nullptr) {
		count++;
		current = current->_next;
	}
	return count;
}
int QueueEmpty(Queue* q)
{
	assert(q);
	return q->_front == nullptr;
}
